package com.ls.controller;

import com.ls.entity.Page;
import com.ls.entity.vo.DiscussPostVo;
import com.ls.service.DiscussPostService;
import com.ls.service.LikeService;
import com.ls.service.UserService;
import com.ls.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author ls
 */
@Controller
public class HomeController implements CommunityConstant {

    @Autowired
    private UserService userService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private LikeService likeService;

    /**
     * 功能描述: <br>
     * 首页
	 * @param model
	 * @param page
	 * @param orderMode
	 * @return {@link String}
     * @author ls
     * @date   2021/8/25 0:13
     */
    @GetMapping({"/", "/index", "index.html"})
    public String toIndex(Model model, Page page,
                          @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {
        //方法调用前，SpringMVC会自动实例化Model和Page，并将Page注入Model
        //所以，在thymeleaf中可以直接访问Page对象中的数据
        //设置总记录数
        page.setTotalCount(discussPostService.queryDiscussPostRows(0));
        //设置路径，便于复用
        page.setPath("/index?orderMode=" + orderMode);

        //页面需要显示用户名，可以用连表查询或者用下面方法
        List<DiscussPostVo> discussPostVos = discussPostService.queryDiscussPosts(0, page.getStartIndex(), page.getPageSize(), orderMode);

        model.addAttribute("discussPostVos", discussPostVos);
        model.addAttribute("orderMode", orderMode);
        return "index";
    }

    /**
     * 错误页面
     * @return
     */
    @GetMapping("/error")
    public String getErrorPage() {
        return "/error/500";
    }

    /**
     * 功能描述: <br>
     * 拒绝访问时的提示页面
	 * @return {@link String}
     * @author ls
     * @date   2021/8/23 15:03
     */
    @GetMapping("/denied")
    public String getDeniedPage() {
        return "/error/404";
    }
}
